package com.Ajava1704.for并发包下的类.Forlock;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;

/**
 * 作者: LCG
 * 日期: 2020/2/11 11:30
 * 描述:
 */
public class ForReentLockRunable implements Runnable {

    private Lock lock;

    public ForReentLockRunable(Lock lock) {
        this.lock = lock;
    }

    private Semaphore semaphore;

    public ForReentLockRunable(Semaphore semaphore) {
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        //lock.lock();//获取锁，获取不到锁的话阻塞到该步骤
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //获取到锁的话再执行逻辑
        for (int i=0;i<5;i++){
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"======运行=========="+i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        //释放锁，获取多少次锁们就要释放多少次
        //lock.unlock();

        semaphore.release();

    }
}
